package cn.itcast.bos.action;

import java.io.IOException;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.PageBean;
import cn.itcast.bos.domain.Staff;
import cn.itcast.bos.service.staff.StaffServiceImpl;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@Scope("prototype")
public class StaffAction extends BaseAction<Staff> implements ModelDriven<Staff> {

	/*private int page;
	private int rows;*/
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}
	/*public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}*/

	public String addStaff(){
		
		staffService.save(model);
		return "list";
	}
	
	//查询派送员信息
	public String pageQuery() throws IOException{
		/*PageBean pageBean = new PageBean();
		pageBean.setCurrentPage(page);
		pageBean.setPageSize(rows);
		
		//使用离线条件对象查询包装封装条件
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Staff.class);
		pageBean.setDetachedCriteria(detachedCriteria);*/
		staffService.pageQuery(pageBean);
		this.writePageBeanToJson(pageBean, new String[]{"detachedCriteria","currentPage","pageSize","decidezones"});
		
		/*JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[]{"detachedCriteria","currentPage","pageSize"});
		JSONObject jsonObject = JSONObject.fromObject(pageBean, jsonConfig);
		String json = jsonObject.toString();
		
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(json);*/
		return NONE;
	}
	
	//作废派送员
	@RequiresPermissions("delete")
	public String delete(){
		staffService.delete(ids);
		return "list";
	}
	
	//还原配送员
	public String restore(){
		staffService.restore(ids);
		return "list";
		
	}
	
	//修改派送员信息
	public String editStaff(){
		Staff staff = staffService.findOne(model.getId());
		staff.setName(model.getName());
		staff.setTelephone(model.getTelephone());
		staff.setStation(model.getStation());
		if (model.getHaspda() != null) {
			staff.setHaspda(model.getStandard());
		} else {
			staff.setHaspda("0");
		}
		staff.setStandard(model.getStandard());
		staffService.update(staff);
		return "list";
	}
	
	//查询未作废派送员
	public String findByListAjax(){
		DetachedCriteria dc = DetachedCriteria.forClass(Staff.class);
		dc.add(Restrictions.eq("deltag", "0"));
		dc.add(Restrictions.isEmpty("decidezones"));
		List<Staff> list = staffService.findByCondition(dc);
		
		this.writeListToJson(list, new String[]{"decidezones","standard","station","deltag","haspda","telephone"});
		return NONE;
	}
}
